package com.xinruke.hostels.dailymanage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwFangjianweixiujilu;
import com.xinruke.hostels.common.po.constants.YwFangjianConstants;
import com.xinruke.hostels.common.po.constants.YwFangjianweixiujiluConstants;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.dailymanage.interfaces.dto.roomrepair.*;
import com.xinruke.hostels.dailymanage.mapper.YwFangjianMapper;
import com.xinruke.hostels.dailymanage.mapper.YwFangjianweixiujiluMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomRepairRecordService {

    @Autowired
    private YwFangjianMapper ywFangjianMapper;

    @Autowired
    private YwFangjianweixiujiluMapper ywFangjianweixiujiluMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Transactional(rollbackFor = Exception.class)
    public ResultVO<Integer> add(RoomRepairRecordAddDTO params) {
        ResultVO<Integer> resultVO = ResultVO.success();

        LoginManageResultVO user = manageUserCache.getLoginManageUserInfo();

        YwFangjianweixiujilu add = new YwFangjianweixiujilu();
        BeanUtils.copyProperties(params, add);
        add.setJiudianId(user.getJiudianId());
        add.setChuangjianshijian(new Date());
        add.setGengxinshijian(new Date());
        add.setWeixiuzhuangtai(YwFangjianweixiujiluConstants.WEIXIUZHUANGTAI_ZHENGZAIJINXING);
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        int insert = ywFangjianweixiujiluMapper.insertSelective(add);
        if(insert == 0){
            return ResultVO.fail();
        }
        resultVO.setData(add.getId());
        return resultVO;
    }

    public ResultVO<RowsDataVO<RoomRepairRecordQueryResultDTO>> list(RoomRepairRecordQueryListDTO params) {

        RowsDataVO<RoomRepairRecordQueryResultDTO> dataVO = new RowsDataVO<>();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        conditions.addCondition("fangjianhao", EConditionOperator.LIKE, params.getFangjianhao());
        conditions.addCondition("kaishishijian", EConditionOperator.GREATER, params.getKaishishijian());
        conditions.addCondition("jieshushijian", EConditionOperator.LESS, params.getJieshushijian());
        conditions.addCondition("weixiuzhuangtai", EConditionOperator.EQULS, params.getWeixiuzhuangtai());

        MyExample example = new MyExample(YwFangjianweixiujilu.class, conditions, params.getOrderFields());
        Page<YwFangjianweixiujilu> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwFangjianweixiujilu> list = ywFangjianweixiujiluMapper.selectByExample(example);

        List<RoomRepairRecordQueryResultDTO> dtoList = list.stream().map(n -> {
            RoomRepairRecordQueryResultDTO dto = new RoomRepairRecordQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(dtoList);
        return ResultVO.success("查询成功", dataVO);
    }

    public ResultVO<RoomRepairRecordQueryResultDTO> detail(RoomRepairRecordQueryDetailDTO params) {

        RoomRepairRecordQueryResultDTO dto  = new RoomRepairRecordQueryResultDTO();
        YwFangjianweixiujilu detail = ywFangjianweixiujiluMapper.selectByPrimaryKey(params.getId());

        if (null == detail) {
            return ResultVO.fail("没有此记录");
        }
        BeanUtils.copyProperties(detail, dto);
        return ResultVO.success("获取详情成功", dto);
    }

    public ResultVO repairComplete(RoomRepairCompleteDTO params) {
        YwFangjianweixiujilu fangjianweixiujilu = ywFangjianweixiujiluMapper.selectByPrimaryKey(params.getId());
        if (fangjianweixiujilu.getWeixiuzhuangtai().equals(YwFangjianweixiujiluConstants.WEIXIUZHUANGTAI_QUXIAO)) {
            return ResultVO.fail("已经取消的状态不能完成");
        }
        // 维修完成是空脏
        ywFangjianMapper.updateStatusById(fangjianweixiujilu.getFangjianId(), YwFangjianConstants.ZHUANGTAI_KONGZANG);

        YwFangjianweixiujilu update = new YwFangjianweixiujilu();
        update.setId(params.getId());
        update.setWeixiuzhuangtai(YwFangjianweixiujiluConstants.WEIXIUZHUANGTAI_WANCHENG);
        update.setWeixiujieguo(params.getWeixiujieguo());
        update.setGengxinshijian(new Date());
        ywFangjianweixiujiluMapper.updateByPrimaryKeySelective(update);
        return ResultVO.success();
    }

    public ResultVO repairCancel(RoomRepairCancelDTO params) {
        YwFangjianweixiujilu fangjianweixiujilu = ywFangjianweixiujiluMapper.selectByPrimaryKey(params.getId());
        if (fangjianweixiujilu.getWeixiuzhuangtai().equals(YwFangjianweixiujiluConstants.WEIXIUZHUANGTAI_WANCHENG)) {
            return ResultVO.fail("已经完成的状态不能取消");
        }
        // 维修取消也改成空脏
        ywFangjianMapper.updateStatusById(fangjianweixiujilu.getFangjianId(), YwFangjianConstants.ZHUANGTAI_KONGZANG);

        YwFangjianweixiujilu update = new YwFangjianweixiujilu();
        update.setId(params.getId());
        update.setWeixiuzhuangtai(YwFangjianweixiujiluConstants.WEIXIUZHUANGTAI_QUXIAO);
        update.setGengxinshijian(new Date());
        ywFangjianweixiujiluMapper.updateByPrimaryKeySelective(update);
        return ResultVO.success();
    }
}
